package com.jorado.etl.service.evaluate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yu.xiao on 2018/10/23.
 */
public class RecommendEvaluate {

    public Map<String, Double> multiple(int N, List<UserInfo> users, Map<String, List<JobInfo>> userJobs, RecallFun fun) {
        double hit = 0;
        double recallAll = 0;
        double precisionAll = 0;
        Map<String, Double> multipleResult = new HashMap<>();
        for (UserInfo user : users) {
            if (userJobs.containsKey(user.getId())) {
                hit += fun.call(N, user, userJobs.get(user.getId()));
                recallAll += userJobs.get(user.getId()).size();
                precisionAll += N;
            }
        }
        double recall = hit / recallAll;
        double precision = hit / precisionAll;
        multipleResult.put("recall", recall);
        multipleResult.put("precision", precision);
        multipleResult.put("F-score:[(2*precision*recall)/(precision+recall)]", (2 * recall * precision) / (recall + precision));
        multipleResult.put("precision/recall", precision / recall);
//        multipleResult.put("F1-score:[ 2*TP/(2*TP + FP + FN)]", (2 * hit) / (recallAll + precisionAll));
        return multipleResult;
    }

    public interface RecallFun {
        double call(int N, UserInfo user, List<JobInfo> jobs);
    }

}
